package licode.unisop.platform.client.authinfo;

import licode.unisop.types.dp.AusUrlItem;
import lombok.Data;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Data
public class AuProperties {
    public static AuProperties EMPTY_PROP = new AuProperties();

    /**
     * 用户标识
     */
    private String userId;

    /**
     * 实际的角色ID
     */
    private List<String> roleIds;

    /**
     * 计算后的角色ID（比实际ID会多）
     */
    private List<String> computeIds;

    /**
     * 角色码
     */
    private List<String> roleCodes;

    /**
     * 权限码
     */
    private List<String> permitCodes;

    private List<AusUrlItem> urls;
    private List<String> forbidMenus;

    private Map<String, String> props;

    public AuProperties() {
        roleIds = new ArrayList<>();
        computeIds = new ArrayList<>();
        roleCodes = new ArrayList<>();
        permitCodes = new ArrayList<>();
        urls = new ArrayList<>();
        props = new HashMap<>();
    }

    public boolean isEmpty() {
        return ((null == roleIds || roleIds.isEmpty()) &&
                (null == permitCodes || permitCodes.isEmpty()) &&
                (null == forbidMenus || forbidMenus.isEmpty()) &&
                (null == urls || urls.isEmpty()));
    }

    public AuProperties clone() {
        AuProperties target = new AuProperties();

        target.setRoleIds(new ArrayList<>(roleIds));
        target.setPermitCodes(new ArrayList<>(permitCodes));
        target.setComputeIds(new ArrayList<>(computeIds));
        target.setUrls(new ArrayList<>(urls));
        target.setProps(new HashMap<>(props));

        return target;
    }

    public String getPropAsString(String key) {
        if (null == props) {
            return "";
        }
        return props.get(key).toString();
    }

    public long getPropAsLong(String key) {
        if (null == props) {
            return 0;
        } else {
            return Long.parseLong(props.get(key));
        }
    }
}
